package com.contactsconverter.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.NotificationManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.contactsconverter.adapter.CTEventAdapter;
import com.contactsconverter.utils.CTUtils;
import com.contactsconverter.R;
import com.difsonapps.contacttranslator.dto.EventDTO;

public class CTEventActivity extends ActionBarActivity {

	public static CTEventAdapter mAdapter;
	public static ArrayList<EventDTO> eventModelList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cthelp);
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		

	
		eventModelList  = (ArrayList<EventDTO>) getIntent().getExtras().get(CTUtils.EVENT_LIST);
		if(eventModelList==null)
			return;
		mAdapter = new CTEventAdapter(CTEventActivity.this, R.layout.event_row, eventModelList);
		
		 NotificationManager mNotificationManager =
			        (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);       
         mNotificationManager.cancel(CTUtils.stringToInt(CTUtils.getPreference(CTUtils.NOTIFICATION_ID, this)));
			    		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cthelp, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		if ( id == android.R.id.home){
			
			Intent upIntent = NavUtils.getParentActivityIntent(this);
			//upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
			NavUtils.navigateUpTo(this, upIntent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		private static View rootView;

		public   ListView lstEvents;
		
		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			rootView = inflater.inflate(R.layout.fragment_ctevent,
					container, false);
			lstEvents = (ListView) rootView.findViewById(R.id.list_events);
			lstEvents.setOnItemClickListener(new OnItemClickListener(){

				public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) 
				{

					EventDTO eventDTO = (EventDTO) mAdapter.getItem(position);					
					if(eventDTO.getUrl()!=null)
					{
						 Intent viewIntent =
						          new Intent("android.intent.action.VIEW",
						            Uri.parse(eventDTO.getUrl()));
						          startActivity(viewIntent);
						
					}

				}
			});
			
			lstEvents.setAdapter(mAdapter);
			return rootView;
		}
	}
	
	@Override
    public void onBackPressed() {
        // finish() is called in super: we only override this method to be able to override the transition
        super.onBackPressed();
        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
    }
}
